<!DOCTYPE HTML>
<html lang="en">
	<head>
		<title>three.js - Lights test</title>
		<meta charset="utf-8">
		<style type="text/css">
			body {
				background:#fff;
				padding:0;
				margin:0;
				overflow:hidden;
				font-family:georgia;
				text-align:center;
			}
			h1 { }
			a { color:skyblue }
			canvas { pointer-events:none; z-index:10; position:relative; }
			#log { position:absolute; top:50px; text-align:left; display:block; z-index:100; }
			#d { text-align:center; margin:1em 0 -15.7em 0; z-index:0; position:relative; display:block }
			.button { background:#000; color:#fff; padding:0.2em 0.5em; cursor:pointer }
			.inactive { background:#999; color:#eee }
		</style>
	</head>
	
	<body>
		<div id="d">
			<h1>Lights test</h1>

			<span id="rcanvas" class="button inactive">2d canvas renderer</span>
			<span id="rwebgl" class="button">WebGL renderer</span>
			<br/>
			
			<p>Using a modified version of <a href="http://github.com/alteredq/three.js">Three.js</a> by mrdoob.
			
			<br/>
			<p>Best viewed in Chrome 8/9 or Firefox 4 using WebGL renderer.
			<p>Canvas renderer is very slow on anything other than Chrome.
		</div>
		
		<pre id="log"></pre>

		<script type="text/javascript" src="../build/Three.js"></script> 
		<!--
		-->

		<!--
		<script type="text/javascript" src="../src/Three.js"></script>
		<script type="text/javascript" src="../src/core/Color.js"></script>
		<script type="text/javascript" src="../src/core/Vector2.js"></script>
		<script type="text/javascript" src="../src/core/Vector3.js"></script>
		<script type="text/javascript" src="../src/core/Vector4.js"></script>
		<script type="text/javascript" src="../src/core/Rectangle.js"></script>
		<script type="text/javascript" src="../src/core/Matrix3.js"></script>
		<script type="text/javascript" src="../src/core/Matrix4.js"></script>
		<script type="text/javascript" src="../src/core/Vertex.js"></script>
		<script type="text/javascript" src="../src/core/Face3.js"></script>
		<script type="text/javascript" src="../src/core/Face4.js"></script>
		<script type="text/javascript" src="../src/core/UV.js"></script>
		<script type="text/javascript" src="../src/core/Geometry.js"></script>
		<script type="text/javascript" src="../src/cameras/Camera.js"></script>
		<script type="text/javascript" src="../src/io/Loader.js"></script>
		<script type="text/javascript" src="../src/lights/Light.js"></script>
		<script type="text/javascript" src="../src/lights/AmbientLight.js"></script>
		<script type="text/javascript" src="../src/lights/DirectionalLight.js"></script>
		<script type="text/javascript" src="../src/lights/PointLight.js"></script>
		<script type="text/javascript" src="../src/objects/Object3D.js"></script>
		<script type="text/javascript" src="../src/objects/Mesh.js"></script>
		<script type="text/javascript" src="../src/objects/Particle.js"></script>
		<script type="text/javascript" src="../src/objects/Line.js"></script>
		<script type="text/javascript" src="../src/materials/LineColorMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshPhongMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshBitmapMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshColorFillMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshColorStrokeMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshFaceMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/ParticleCircleMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/ParticleBitmapMaterial.js"></script>
		<script type="text/javascript" src="../src/scenes/Scene.js"></script>
		<script type="text/javascript" src="../src/renderers/Projector.js"></script>
		<script type="text/javascript" src="../src/renderers/CanvasRenderer.js"></script>
		<script type="text/javascript" src="../src/renderers/SVGRenderer.js"></script>
		<script type="text/javascript" src="../src/renderers/WebGLRenderer.js"></script>
		<script type="text/javascript" src="../src/renderers/renderables/RenderableFace3.js"></script>
		<script type="text/javascript" src="../src/renderers/renderables/RenderableFace4.js"></script>
		<script type="text/javascript" src="../src/renderers/renderables/RenderableParticle.js"></script>
		<script type="text/javascript" src="../src/renderers/renderables/RenderableLine.js"></script>
		-->

		<script type="text/javascript" src="../src/extras/primitives/Sphere.js"></script>
		<script type="text/javascript" src="../src/extras/primitives/Plane.js"></script>

		<script type="text/javascript" src="js/Stats.js"></script>

		<script type="text/javascript">

			var SCREEN_WIDTH = window.innerWidth / 2;
			var SCREEN_HEIGHT = window.innerHeight;
			var FLOOR = -250;

			var container;
			var stats;

			var camera;
			var scene;
			var canvasRenderer, webglRenderer;

			var mesh, zmesh, lightMesh, geometry;

			var directionalLight, pointLight;

			var mouseX = 0;
			var mouseY = 0;

			var windowHalfX = window.innerWidth >> 1;
			var windowHalfY = window.innerHeight >> 1;

			var render_canvas = 1, render_gl = 1;
			var has_gl = 0;
			
			var bcanvas = document.getElementById("rcanvas");
			var bwebgl = document.getElementById("rwebgl");
			
			document.addEventListener('mousemove', onDocumentMouseMove, false);

			init();
			
			loop();
			
			//render_canvas = !has_gl;
			bwebgl.style.display = has_gl ? "inline" : "none";
			bcanvas.className = render_canvas ? "button" : "button inactive";
			
			setInterval(loop, 1000/60);
			
			function init() {

				container = document.createElement('div');
				document.body.appendChild(container);

				camera = new THREE.Camera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
				camera.position.z = 500;
				camera.updateMatrix();

				scene = new THREE.Scene();
				
				
				// SPHERES
				
				sphere = new Sphere( 100, 16, 8, 0 );
				for (var i=0; i<30; i++) {
					mesh = new THREE.Mesh( sphere, new THREE.MeshColorFillMaterial( 0xffffff ) );
					mesh.position.x = 500 * (Math.random() - 0.5);
					mesh.position.y = 500 * (Math.random() - 0.5);
					mesh.position.z = 500 * (Math.random() - 0.5);
					mesh.scale.x = mesh.scale.y = mesh.scale.z = 0.25 * (Math.random() + 0.5);
					mesh.overdraw = true;
					mesh.updateMatrix();
					scene.addObject(mesh);
				}
				

				// LIGHTS

				var ambient = new THREE.AmbientLight( 0x101010 );
				scene.addLight( ambient );

				directionalLight = new THREE.DirectionalLight( 0xffffff );
				directionalLight.position.y = -70;
				directionalLight.position.z = 100;
				directionalLight.position.normalize();
				scene.addLight( directionalLight );

				pointLight = new THREE.PointLight( 0xffaa00 );
				pointLight.position.x = 0;
				pointLight.position.y = 0;
				pointLight.position.z = 0;
				scene.addLight( pointLight );


				sphere = new Sphere( 100, 16, 8, 1 );
				lightMesh = new THREE.Mesh( sphere, new THREE.MeshColorFillMaterial( 0xffaa00 ) );
				lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05;
				lightMesh.position = pointLight.position;
				lightMesh.overdraw = true;
				lightMesh.updateMatrix();
				scene.addObject(lightMesh);


				if( render_canvas ) {
					canvasRenderer = new THREE.CanvasRenderer();
					canvasRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
					container.appendChild( canvasRenderer.domElement );
				}

				if ( render_gl ) {
					try {
						webglRenderer = new THREE.WebGLRenderer( scene );
						webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
						container.appendChild( webglRenderer.domElement );
						has_gl = 1;
					}
					catch (e) {
					}
				}

				stats = new Stats();
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.top = '0px';
				stats.domElement.style.zIndex = 100;
				container.appendChild( stats.domElement );
				
				bcanvas.addEventListener("click", toggleCanvas, false);
				bwebgl.addEventListener("click", toggleWebGL, false);


				var loader = new THREE.Loader();
				//loader.loadAsciiOld( "obj/torus/Torus.js", function() { createScene( new Torus() ) } );
				//loader.loadAscii( "obj/torus/Torus_slim.js", function( geometry ) { createScene( geometry ) } );
                loader.loadBinary( "obj/torus/Torus_bin.js", function( geometry ) { createScene( geometry ) }, "obj/torus" );

			}

			function createScene( geometry ) {
				
				zmesh = new THREE.Mesh( geometry, new THREE.MeshColorFillMaterial( 0xffffff ) );
				zmesh.position.x = 0;
				zmesh.position.z = 0;
				zmesh.position.y = 0;
				zmesh.scale.x = zmesh.scale.y = zmesh.scale.z = 100;
				zmesh.overdraw = true;
				zmesh.updateMatrix();
				scene.addObject(zmesh);
				
			}

			
			function onDocumentMouseMove(event) {

				mouseX = ( event.clientX - windowHalfX );
				mouseY = ( event.clientY - windowHalfY );

			}

			var r = 0;
			
			function loop() {

				camera.position.x += ( mouseX - camera.position.x ) * .05;
				camera.position.y += ( - mouseY - camera.position.y ) * .05;
				camera.updateMatrix();
				
				
				for(var i=0; i<scene.objects.length; ++i) {
					if ( i%3 == 1 )
						scene.objects[i].rotation.x += 0.05;
					else if ( i%3 == 2 )
						scene.objects[i].rotation.y += 0.05;
					else if ( i%3 == 0 )
						scene.objects[i].rotation.z += 0.05;
						
					scene.objects[i].updateMatrix();
				}
				
				
				lightMesh.position.x = 200*Math.cos(r);
				lightMesh.position.z = 200*Math.sin(r);
				lightMesh.updateMatrix();

				r += 0.1;
				
				if ( render_canvas ) canvasRenderer.render( scene, camera );
				if ( render_gl && has_gl ) webglRenderer.render( scene, camera );

				stats.update();

			}

			function log(text) {
			
				var e = document.getElementById("log");
				e.innerHTML = text + "<br/>" + e.innerHTML;
				
			}
			
			function toggleCanvas() {
			
				render_canvas = !render_canvas;
				bcanvas.className = render_canvas ? "button" : "button inactive";
				
				render_gl = !render_canvas;
				bwebgl.className = render_gl ? "button" : "button inactive";
				
				if( has_gl )
					webglRenderer.domElement.style.display = render_gl ? "block" : "none";
				
				canvasRenderer.domElement.style.display = render_canvas ? "block" : "none";
				
			}
			
			function toggleWebGL() {
			
				render_gl = !render_gl;
				bwebgl.className = render_gl ? "button" : "button inactive";
				
				render_canvas = !render_gl;
				bcanvas.className = render_canvas ? "button" : "button inactive";
				
				if( has_gl )
					webglRenderer.domElement.style.display = render_gl ? "block" : "none";
					
				canvasRenderer.domElement.style.display = render_canvas ? "block" : "none";
				
			}
		</script>

	</body>
</html>
